﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Tomato.Libraries.Patterns
{

    /// <summary>
    /// Generic singleton.
    /// </summary>
    /// <typeparam name="T"> Type which has the default non-parameter constructor. </typeparam>
    public class Singleton<T>
        where T : new( )
    {

        /// <summary>
        /// Static constructor.
        /// </summary>
        static Singleton( ) { }

        /// <summary>
        /// Disbale constructors.
        /// </summary>
        private Singleton( ) { }

        public static T Instance
        {

            get
            {
                return Nested.Instance;
            }

        }

        internal class Nested
        {

            /// <summary>
            /// Static constructor.
            /// </summary>
            static Nested( ) { }

            /// <summary>
            /// Note: If an exception is thrown in T( ),
            /// there will be no chance to try again.
            /// </summary>
            internal static readonly T Instance = new T( );

        }

    }

}